﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using RaygunDiplomacy.Entities.Characters;
using System.Windows;
using RaygunDiplomacy.Entities;

namespace RaygunDiplomacy.ControllersModule
{
   class HumanController : EntityController
   {
      #region Commands

      public class Command
      {
         public string Name { get; private set; }

         public Action<HumanController> KeyDownAction { get; private set; }
         public Action<HumanController> KeyUpAction { get; private set; }

         public Command(string name, Action<HumanController> keyDownAction, Action<HumanController> keyUpAction)
         {
            this.Name = name;
            this.KeyDownAction = keyDownAction;
            this.KeyUpAction = keyUpAction;
         }
      }

      static Dictionary<Key, Command> commandMappings = new Dictionary<Key, Command>();

      static void SetupCommands()
      {
         commandMappings.Add(System.Windows.Input.Key.Left, TurnLeftCommand);
         commandMappings.Add(System.Windows.Input.Key.Right, TurnRightCommand);
         commandMappings.Add(System.Windows.Input.Key.Up, MoveForwardCommand);
         commandMappings.Add(System.Windows.Input.Key.Down, MoveBackwardCommand);
         commandMappings.Add(System.Windows.Input.Key.LeftShift, RunCommand);
         commandMappings.Add(System.Windows.Input.Key.RightShift, RunCommand);
      }

      #region Movement

      public static readonly Command TurnLeftCommand = new Command("Turn Left", 
         hc => hc.character.Navigation |= NavigationFlags.TurningLeft,
         hc => hc.character.Navigation ^= NavigationFlags.TurningLeft);

      public static readonly Command TurnRightCommand = new Command("Turn Right",
         hc => hc.character.Navigation |= NavigationFlags.TurningRight,
         hc => hc.character.Navigation ^= NavigationFlags.TurningRight);

      public static readonly Command MoveForwardCommand = new Command("Move Forward",
         hc => hc.character.Navigation |= NavigationFlags.MovingForward,
         hc => hc.character.Navigation ^= NavigationFlags.MovingForward);

      public static readonly Command MoveBackwardCommand = new Command("Move Backward",
         hc => hc.character.Navigation |= NavigationFlags.MovingBackward,
         hc => hc.character.Navigation ^= NavigationFlags.MovingBackward);

      public static readonly Command RunCommand = new Command("Run",
         hc => hc.character.Navigation |= NavigationFlags.MovingBackward,
         hc => hc.character.Navigation ^= NavigationFlags.MovingBackward);

      #endregion

      #endregion

      #region Fields

      ICharacter character;

      #endregion

      #region Properties

      internal ICharacter Character
      {
         get { return character; }
         set { character = value; }
      }

      #endregion

      #region Constructor

      public HumanController(Window window)
      {
         window.KeyDown += new KeyEventHandler(window_KeyDown);
         window.KeyUp += new KeyEventHandler(window_KeyUp);
      }

      static HumanController()
      {
         SetupCommands();
      }

      #endregion

      #region Methods

      void window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
      {
         if (commandMappings.ContainsKey(e.Key) && commandMappings[e.Key].KeyDownAction != null)
         {
            commandMappings[e.Key].KeyDownAction(this);
         }
      }

      void window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
      {
         if (commandMappings.ContainsKey(e.Key) && commandMappings[e.Key].KeyUpAction != null)
         {
            commandMappings[e.Key].KeyUpAction(this);
         }
      }

      #endregion
   }
}
